<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<cl-flex1 />
			<!-- 条件搜索 -->
			<cl-search ref="Search" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" setup>
defineOptions({
	name: 'user-proxy'
});

import { useCrud, useTable, useUpsert, useSearch } from '@cool-vue/crud';
import { useCool } from '/@/cool';
import { useI18n } from 'vue-i18n';
import { ref } from 'vue';

const { service } = useCool();
const { t } = useI18n();

/* 所有商户 */
const allStore = ref<any[]>([]);

/* 获取所有role为2的用户 */
const getAgentList = async () => {
	const children = await service.user.info.list({
		role: 2
	});

	console.log('children', children);

	allStore.value = children?.map(_c => {
		return {
			label: `${_c.nickName ?? '-'} (手机号：${_c.phone})`,
			value: _c.id
		};
	});
};

getAgentList();

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: t('昵称'),
			prop: 'nickName',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: t('手机号（登陆账号）'),
			prop: 'phone',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: t('密码'),
			prop: 'password',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: t('商户号（在拉卡拉获取）'),
			prop: 'merchantNo',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: t('终端号（在拉卡拉获取）'),
			prop: 'terminalNo',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},

		() => {
			return {
				label: '推荐人',
				prop: 'referrer',
				component: {
					name: 'el-select',
					options: allStore
				}
			};
		}
	],
	async onSubmit(data, { done, close, next }) {
		// 如果是编辑
		if (data.id) {
			next({
				...data
				// role: 2
			});

			return;
		} else {
			service.user.info.createMerchant(data).then(() => {
				done();
			close();

			// 刷新
			refresh()
			});
		}

		// done 关闭加载状态
		// close 关闭弹窗
	}
});

// cl-table
const Table = useTable({
	columns: [
		{ type: 'selection' },
		{ label: t('昵称'), prop: 'nickName', minWidth: 120 },
		{ label: t('手机号'), prop: 'phone', minWidth: 120 },
		{
			label: t('创建时间'),
			prop: 'createTime',
			minWidth: 170,
			sortable: 'desc',
			component: { name: 'cl-date-text' }
		},
		{ type: 'op', buttons: ['edit', 'delete'] }
	]
});

// cl-search
const Search = useSearch({
	items: [
		{
			label: ' ',
			prop: 'keyWord',
			component: {
				name: 'el-input',
				props: {
					clearable: true,
					placeholder: t('请输入手机号')
				}
			}
		},
		{
			label: ' ',
			prop: 'agentLevel',
			component: {
				name: 'el-input',
				props: {
					clearable: true,
					placeholder: t('请输入代理级别')
				}
			}
		}
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.user.info,
		async onRefresh(params, { next, done, render }) {
			// 1 默认调用
			const { list } = await next({
				...params,
				role: 2
			});

			// 渲染数据
			render(list);
		}
	},
	app => {
		app.refresh();
	}
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
